package controllers;

import controllers.auth.Secure;
import controllers.basic.LoginCtrl;
import me.chanjar.weixin.common.util.StringUtils;
import models.admin.AdminMenu;
import models.admin.AdminUser;
import play.Logger;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by buhaoba on 2017/5/24.
 */
@With(Secure.class)
public class BaseController extends Controller {

    @Before
    public static void menu(String menuId) {
        AdminUser adminUser = Secure.getLoginUser();
        Logger.info("AdminUser.name : %s | adminUser: %s" , adminUser.loginName ,adminUser);
        List<Map<String, Object>> menuList = AdminMenu.loadMenuByUserId(adminUser.id);
//        Logger.info("MenuList : %s" , menuList);
        if(adminUser == null)
            LoginCtrl.login();
        if(StringUtils.isNotBlank(menuId)) {
            session.put("menuId" , menuId);
        }

        if(StringUtils.isBlank(menuId)) {
            if(session.get("menuId") != null) {
                menuId = session.get("menuId");
            }
        }

        renderArgs.put("menuId" , menuId);
        renderArgs.put("menuList" , menuList);
        renderArgs.put("adminUser" , adminUser);
    }

}
